var util = require('../../utils/util.js');
var api = require('../../config/api.js');

Page({
  data: {
    allProjects: [],
    items:[1,2,3]
   
  },
  onLoad: function(options) {
    
  },

  onTouchDel(e){
    console.log('--------------del:'+e.currentTarget.dataset.id)
    var pid = 'pid_'+e.currentTarget.dataset.id
    var title = JSON.parse(wx.getStorageSync(pid)).title
    var that = this
    wx.showModal({  
      title: '确认',  
      content: '确定删除：'+title+'？',  
      success (res) {  
        if (res.confirm) {  
          wx.removeStorageSync(pid)
          that.initHistory()
        } else if (res.cancel) {  
          console.log('用户点击取消')  
        }  
      }  
    })
    

    
  },

  onTouchEdit(e){
    console.log('--------------edit:'+e.currentTarget.dataset.id)
    var url = '/pages/softProject/softProject?'+'&project_id='+e.currentTarget.dataset.id
    wx.navigateTo({
      url: url,
    })
  },

  onReady: function() {
    // 页面渲染完成
    
  },
  onShow: function() {
    console.log('-----------onShow--------')
    // 页面显示
    this.initHistory()
  },
  onHide: function() {
    // 页面隐藏
  },
  onUnload: function() {
    // 页面关闭
  },
  onPullDownRefresh: function(){
    console.log('---------')
  },

  initHistory: function(){
    var all = wx.getStorageInfoSync()
    var allKeys = all.keys
    
    var allProjects = []
    for (var t = 0; t < allKeys.length; t++) {
        var key = allKeys[t]
        if(key.startsWith('pid_')){
            allProjects.push(JSON.parse(wx.getStorageSync(key)));
        }
    }
    for(var t2 = 0;t2<allProjects.length;t2++){
        if(allProjects[t2].edit_time===undefined || allProjects[t2].edit_time==null){
            allProjects[t2].edit_time = 0;
        }
    }

    var tempSortSwap = null;
    for(var i=0;i<allProjects.length;i++){
        for(var j=0;j<allProjects.length;j++){
            if(allProjects[i].edit_time>allProjects[j].edit_time){
                tempSortSwap = allProjects[i]
                allProjects[i] = allProjects[j]
                allProjects[j] = tempSortSwap
            }
        }
    }


    for(var i=0;i<allProjects.length;i++){
      var nowProject = allProjects[i]
      
      var timestamp = nowProject.create_time;
      var date = new Date(timestamp);
      var formattedDate = util.formatTimeToMin(date)
      nowProject.ctime = formattedDate

      timestamp = nowProject.edit_time;
      date = new Date(timestamp);
      formattedDate = util.formatTimeToMin(date)
      nowProject.mtime = formattedDate
    }
    
    this.setData({
      allProjects: allProjects
    })
    return;

    for(var n=0;n<=5;n++){
        if(n>=allProjects.length){
            $('#history_div_'+n).hide()
        }else {
            $('#history_div_'+n).show()
        }
    }
    for(var k=0;k<allProjects.length;k++){
        var nowProject = allProjects[k]
        if(k>=6){
            localStorage.removeItem('pid_'+nowProject.project_id)
        }
        document.getElementById('history_pid_'+k).textContent = nowProject.project_id
        if(nowProject.edit_time!==undefined && nowProject.edit_time!=null && nowProject.edit_time!==0){
            var timestamp = nowProject.edit_time;
            var date = new Date(timestamp);
            const formattedDate = date.toLocaleString();
            document.getElementById('history_edit_time_'+k).textContent = formattedDate
        }

        document.getElementById('history_name_'+k).innerHTML = nowProject.title+'<br>'+nowProject.version+' '+nowProject.author_name
        $('#history_div_'+k).on('click',{index: k},
            function (event){
                console.log(event.data.index)
                var nowPid = document.getElementById('history_pid_'+event.data.index).textContent
                window.location = ('soft_project.php?project_id='+(nowPid));
            }
        )

        //history_delete_5
        $('#history_delete_'+k).on('click',{index: k},
            function (event){
                console.log(event.data.index)
                var nowPid = document.getElementById('history_pid_'+event.data.index).textContent
                console.log(nowPid)
                event.stopPropagation()
                layui.use(['layer'],function (){
                    layer.confirm('确认删除此条记录？', {icon: 3, title:'提示'}, function(index){
                        localStorage.removeItem('pid_'+nowPid)
                        initHistory();
                        layer.close(index);
                    });
                })
            }
        )
    }
  },
  
})